package cn.ava.pet.controller;

import cn.ava.basic.annotation.PreAuthorize;
import cn.ava.basic.utils.LoginContext;
import cn.ava.org.domain.Department;
import cn.ava.org.domain.Employee;
import cn.ava.org.service.IEmployeeService;
import cn.ava.pet.domain.PetDetail;
import cn.ava.pet.service.IPetService;
import cn.ava.pet.domain.Pet;
import cn.ava.pet.query.PetQuery;
import cn.ava.basic.utils.AjaxResult;
import cn.ava.basic.utils.PageList;
import cn.ava.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName: PetController
 * @Description: 宠物管理模块
 * @Author: Avavaava
 * @Date: 2022/7/26 16:40
 * @version: 23.53
 *
 */
@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    public IPetService petService;

    @Autowired
    private IEmployeeService employeeService;
    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet, HttpServletRequest request){
        try {
            Logininfo loginUser = (Logininfo)LoginContext.getLoginUser(request);
            if (loginUser != null){
                Employee employee = employeeService.getByLogininfoId(loginUser.getId());
                pet.setShopId(employee.getShopId());
            }
            if( pet.getId()!=null)
                petService.update(pet);
            else
                petService.add(pet);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace(); 
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Pet getById(@PathVariable("id")Long id)
    {
        return petService.getById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Pet> list(){
        return petService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping()
    public PageList<Pet> json(@RequestBody PetQuery query)
    {
        return petService.queryData(query);
    }


    /**
     * @Title: offsale
     * @Description: 宠物下架
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/26 16:42
     * @param:
     * @return:
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }

    /**
     * @Title: onsale
     * @Description: 宠物上架
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/26 16:41
     * @param:
     * @return:
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onsale(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }

    /**
     * 查看所有的员工信息
     * @return
     */
    @GetMapping("/detail/{id}")
    public PetDetail getPetDetail(@PathVariable("id")Long id){
        return petService.getPetDetail(id);
    }

    @PatchMapping
    public AjaxResult batchDelete(@RequestBody Long[] id) {
        try {
            petService.batchDelete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("服务器崩溃！").setSuccess(false);
        }
    }


}
